<?php
namespace app\api\controller;
use think\Controller;
use think\Session;
header("charset=utf-8");
class Lessons extends Controller
{
    public function getList(){
        $id=input('id');
        $result=db('lessons')->select();
        if($result)
        {
            echo json_encode($result);
        }
        else
            echo json_encode(['0']);
        die();

    }
        public function enroll()
    {
        $id=input('id');
        if(Session::has('uid'))
        {
            $a=db('buy')->where('uid',Session::get('uid'))->where('lid',$id)->find();
            if($a){
                echo json_encode(['status'=>0,'message'=>'You have signed up for this course
                ']); 
                //(您已报名此课程)
                die();
            }

            $result=db('buy')->insert(['uid'=>Session::get('uid'),'lid'=>$id,'btime'=>date("Y-m-d H:i")
]);
            if($result)
            {
            echo json_encode(['status'=>1,'message'=>'Success']);
            }
            else
            echo json_encode(['status'=>0,'message'=>'Database operation failed, please contact the administrator']);
        //(数据库操作失败，请联系管理员)
        }
        else
             echo json_encode(['status'=>0,'message'=>'You are not landing']);
        die();
    }
     public function cancel()
    {
       $id=input('id');
        if(Session::has('uid'))
        {
            $a=db('buy')->where(['lid'=>$id,'uid'=>Session::get('uid')])->find();
            if(!$a){
                echo json_encode(['status'=>0,'message'=>'You did not sign up for this course
                  ']); 
                //您没报名此课程
                die();
            }
            
            $result=db('buy')->delete($a);
            if($result)
            {
            echo json_encode(['status'=>1,'message'=>'Success']);
            }
            else
            echo json_encode(['status'=>0,'message'=>'Database operation failed, please contact the administrator']);
        //数据库操作失败，请联系管理员
        }
        else
             echo json_encode(['status'=>0,'message'=>'You are not landing']);
         //您没登录
        die();
    }
}
